package customer.zspt.cn.customer.components.chat.ChatOrderContent.OrderElementPg;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import org.json.JSONArray;
import org.json.JSONException;

import customer.zspt.cn.customer.R;

/**
 * Created by Administrator on 2018/8/16 0016.
 */

public class TimeIntervalStartView extends BaseView implements View.OnClickListener{
    private  LinearLayout end_ll;
    private  LinearLayout start_ll;
    private  TextView time_Start;
    private  TextView timer_Sttitle;
    private  TextView timer_Edtitle;
    private  TextView time_End;
    private TimePicker time_picker;
     //开始区间
    private String startTime;
    //结束区间
    private String endTime;
    public TimeIntervalStartView(final Context context,String titleVaules,JSONArray defalutValues,
                                 boolean isInterval,String timeValue){
        super(context,titleVaules);
        View.inflate(context,
                R.layout.chat_order_content_timeinterval_strart,this);
        start_ll = (LinearLayout) findViewById(R.id.start_ll);
        end_ll = (LinearLayout) findViewById(R.id.end_ll);
        start_ll.setOnClickListener(this);
        end_ll.setOnClickListener(this);
        //开始区间
        timer_Sttitle = (TextView) findViewById(R.id.timer_Sttitle);
        timer_Sttitle.setText("时间区间[开始]");
        time_Start = (TextView)findViewById(R.id.time_Start);

        if (isInterval){
            end_ll.setVisibility(GONE);
            timer_Sttitle.setText(titleVaules);
            time_Start.setText(timeValue);
        }else{
            try {
                time_Start.setText(defalutValues.get(0).toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //结束区间
            timer_Edtitle = (TextView) findViewById(R.id.timer_Edtitle);
            timer_Edtitle.setText("时间区间[结束]");
            time_End = (TextView)findViewById(R.id.time_End);
            try {
                time_End.setText(defalutValues.get(1).toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }
    /**弹出时间选择框*/
    private void showDialog(Context context,String resultStr,final TextView contentTv) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        View view = View.inflate(context,
                R.layout.chat_order_content_timeinterval_timepicker,null);
        time_picker = (TimePicker) view.findViewById(R.id.time_picker);
        dialog.setView(view);
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int mHours = time_picker.getCurrentHour();
                int mMin = time_picker.getCurrentMinute();
                contentTv.setText(((mHours < 10) ? ("0" + mHours) : mHours) + ":"
                        + ((mMin < 10) ? ("0" + mMin) : mMin)+">");
            }
        });
        dialog.setNegativeButton("取消",null);
        dialog.show();
    }
    @Override
    public String result() {
        return time_Start.getText().toString();
    }

    @Override
    public void onClick(View v) {
         switch (v.getId()){
             case R.id.start_ll:
                 showDialog(getContext(),startTime,time_Start);
                 break;
             case R.id.end_ll:
                 showDialog(getContext(),endTime,time_End);
                 break;
         }
    }
}
